package com.smartalarmclock.UI;

import java.util.ArrayList;
import java.util.HashMap;
import com.smartalarmclock.Logic.AcManager;
import com.smartalarmclock.Model.IProfile;
import com.smartalarmclock.UI.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ProfileActivity extends Activity {
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile_screen);
        
        // set listener for buttons
        final Button backButton = (Button)findViewById(R.id.button_back);
        backButton.setOnClickListener(backButtonClickListener);
        
        final Button newProfileButton = (Button)findViewById(R.id.button_new);
        newProfileButton.setOnClickListener(newProfileButtonClickListener);
        
        refreshProfileList();
    }
    
    private OnItemClickListener profileListItemClickListener = new OnItemClickListener() {
     	private static final String TAG = "OnItemClickListener";

        @Override
        public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
        {
    	    Log.v(TAG, "Clicked on profile (Ofer)");

        	final IProfile item = (IProfile)parent.getItemAtPosition(position);
          
        	// start profile details activity for editing a profile 
      		Intent newProfileActivityIntent = new Intent(ProfileActivity.this, CreateNewProfileActivity.class);
      		newProfileActivityIntent.putExtra(Messages.PROFILE_DETAILS_IS_NEW, false);
      		newProfileActivityIntent.putExtra(Messages.PROFILE_DETAILS_PROFILE_ID, item.GetId());
      		startActivity(newProfileActivityIntent);
      		
      		final ListView profileListView = (ListView)findViewById(R.id.listView_list);
            ArrayAdapter<IProfile> arrayAdapter = (ArrayAdapter<IProfile>)profileListView.getAdapter();

			arrayAdapter.notifyDataSetChanged();
			
        }
    };
    
    private OnClickListener newProfileButtonClickListener = new OnClickListener()
    {
    	@Override
        public void onClick(View v)
        {   
    		
        	// start profile details activity for a new profile 
    		Log.v("ERROR", "Before creating Intent");
    		
        	Intent newProfileActivityIntent = new Intent(v.getContext(), CreateNewProfileActivity.class);
        	newProfileActivityIntent.putExtra(Messages.PROFILE_DETAILS_IS_NEW, true);
        	newProfileActivityIntent.putExtra(Messages.PROFILE_DETAILS_PROFILE_ID, -1);
        	Log.v("ERROR", "Before Activating newProfileActivityIntent");
        	
        	startActivity(newProfileActivityIntent);
        	
        	// update the active profiles from the AcManager
      		final ListView profileListView = (ListView)findViewById(R.id.listView_list);
            ArrayAdapter<IProfile> arrayAdapter = (ArrayAdapter<IProfile>)profileListView.getAdapter();
			arrayAdapter.notifyDataSetChanged();	
        }
    };
    
    @Override
    protected void onResume()
    {
    	refreshProfileList();
    	super.onResume();
    }
    
    private void refreshProfileList()
    {
    	// set profile list adapter and items on-click listener
    	final ListView profileListView = (ListView)findViewById(R.id.listView_list);
		profileListView.setAdapter(new ProfileArrayAdapter(this));
        profileListView.setOnItemClickListener(profileListItemClickListener);
    }
    
    private OnClickListener backButtonClickListener = new OnClickListener()
    {
        public void onClick(View v)
        {   
             finish();
        }
    };
    
    private class ProfileArrayAdapter extends ArrayAdapter<IProfile>
    {
    	private Context m_Context = null;
    	HashMap<IProfile, Integer> m_IdMap = null;
    	
    	public ProfileArrayAdapter(Context context)
    	{
    		super(context, R.layout.main_menu_item_details_view, AcManager.GetInstance().GetActiveProfiles(context));
    		m_Context = context;
    		
    		updateProfilesData();
    	}
    	
    	public View getView(int position, View convertView, ViewGroup parent) {
    		ViewHolder holder;
    		if (convertView == null) {
    			convertView = LayoutInflater.from(m_Context).inflate(R.layout.profile_list_item, null);
    			holder = new ViewHolder();
    			holder.txt_itemName = (TextView)convertView.findViewById(R.id.profile_list_item_text);
    			holder.itemImage = (ImageView)convertView.findViewById(R.id.profile_list_item_image);

    			convertView.setTag(holder);
    		} else {
    			holder = (ViewHolder) convertView.getTag();
    		}
    		
    		holder.txt_itemName.setText(getItem(position).GetName());
    		holder.itemImage.setImageResource(R.drawable.profile);

    		return convertView;
    	}
    	
    	private void updateProfilesData()
    	{
    		ArrayList<IProfile> profilesList = AcManager.GetInstance().GetActiveProfiles(m_Context);
    		m_IdMap = new HashMap<IProfile, Integer>();
    		// set profiles in id-map
    		int i = 0;
    		for (IProfile profileItem : profilesList)
    		{
    			m_IdMap.put(profileItem, i++);
    		}
    	}
    	
    	@Override
    	public long getItemId(int position)
    	{
    		IProfile item = getItem(position);
    		return m_IdMap.get(item);
    	}
    	
    	@Override
    	public boolean hasStableIds()
    	{
    		return true;
    	}
    	
    	@Override
    	public void notifyDataSetChanged()
    	{
    		updateProfilesData();
    		super.notifyDataSetChanged();
    	}
    }
    
    static class ViewHolder
    {
		TextView txt_itemName;
		ImageView itemImage;
	}
}
